﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DSS_XNA.Engine.Log
{
    public static class Logger
    {
        #region Properties

        public const string DefaultLogPath = @"\log\log.txt";

        public static string LogPath
        {
            get { return Logger._logPath; }
            set { Logger._logPath = value; }
        }
        private static string _logPath = DefaultLogPath;

        private static FileStream Fp
        {
            get { return Logger._fp; }
            set { Logger._fp = value; }
        }
        private static FileStream _fp = null;

        private static TextWriter LogStream
        {
            get { return Logger._logStream; }
            set { Logger._logStream = value; }
        }
        private static TextWriter _logStream = null;

        public static LogLevel LogLevel
        {
            get { return Logger._logLevel; }
            set { Logger._logLevel = value; }
        }
        private static LogLevel _logLevel = LogLevel.Minor;

        #endregion

        #region Static Methods

        public static void Log(string message, LogLevel level)
        {
            if (level < LogLevel)
                return;

            if (Fp == null)
            {
                CreateLogger();
                if (Fp == null)
                {
                    LogCritical(String.Format("Failed to create Logger: {0}", LogPath));
                    return;
                }
            }

            LogStream.WriteLine(String.Format("{0} - {1} - {2}", DateTime.Now.ToLongTimeString(), level, message));
            LogStream.Flush();
        }

        public static void LogCritical(string message)
        {
            Console.Write(message);
        }

        private static void CreateLogger()
        {
            _fp = File.Open(LogPath, FileMode.Create);
            _logStream = new StreamWriter(_fp);
        }

        #endregion
    }
}
